package org.goat.caprabank.client;

import org.goat.caprabank.shared.entity.User;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DoubleBox;
import com.google.gwt.user.client.ui.LongBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

public class TransferPanel extends Composite {
	interface TransferPanelUiBinder extends UiBinder<Widget, TransferPanel> {
	}
	private static TransferPanelUiBinder uiBinder = GWT.create(TransferPanelUiBinder.class);

	private final Caprabank caprabank;

	public TransferPanel(Caprabank caprabank) {
		initWidget(uiBinder.createAndBindUi(this));
		this.caprabank = caprabank;
	}

	@UiField
	LongBox targetAccountTextBox;

	@UiField
	DoubleBox amountTextBox;

	@UiField
	TextArea commentTextArea;

	@UiHandler("transferButton")
	void onClickTransfer(ClickEvent e) {
		boolean isValid = Validation.validateValue(targetAccountTextBox, "target account") && Validation.validateValue(amountTextBox, "amount");
		if (isValid) {
			caprabank.bankService.transfer(targetAccountTextBox.getValue(), (long) (amountTextBox.getValue() * 100) / 1, commentTextArea.getText(), new DefaultCallback<User>() {

				@Override
				public void onSuccess(User result) {
					caprabank.showAccountDetailsPage(result);
				}
			});
		}
	}

	@UiHandler("cancelButton")
	void onClickCancel(ClickEvent e) {
		caprabank.bankService.getDetails(new DefaultCallback<User>() {

			@Override
			public void onSuccess(User result) {
				caprabank.showAccountDetailsPage(result);
			}
		});
	}
}
